﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using 纺织贸易管理系统;

namespace BLL
{
   public static  class 委外发货单BLL
    {
        public static void 保存单据(DanjuTable danju,List<danjumingxitable > danjumingxitables ,List <JuanHaoTable > juanHaoTables)
        {
            if(SysInfo.GetInfo.own !="审核制")
            {
                danju.zhuangtai = "已审核";
            }
            else
            {
                danju.zhuangtai = "未审核";
            }
            Connect.dbcontext.Insertable(danju).ExecuteCommand();
            foreach (var d in danjumingxitables.Where(x=>x.pingming!=null).ToList ())
            {
                d.danhao = danju.dh;
            } 
            Connect.dbcontext.Insertable(danjumingxitables.Where(x=>!string.IsNullOrEmpty(x.pingming )).ToList()).ExecuteCommand();
            foreach (var j in juanHaoTables )
            {
                var madan = new FahuoDan()
                {
                    bh = j.SampleNum,
                    pm = j.SampleName,
                    GG=j.guige ,
                    gh = j.GangHao,
                    ys = j.yanse,
                    jh = j.JuanHao,
                    mis = j.biaoqianmishu.ToString(),
                    color = j.yanse,
                    Gangjuanhao = j.PiHao.ToString(),
                    dh = danju.dh,
                    rq=danju.rq ,
                };
                j.Danhao = danju.dh;
                j.state = 1;
               Connect.dbcontext.Insertable(madan).ExecuteCommand ();
                //JuanHaoTableService.UpdateJuanHaoTable(j, x => x.JuanHao == j.JuanHao);
               
            }

            Connect.dbcontext.Updateable(juanHaoTables).ExecuteCommand();
            if(SysInfo.GetInfo.own!="审核制")
            {
                审核(danju.dh);
            }
        }
        public static void 修改单据(DanjuTable danju, List<danjumingxitable> danjumingxitables, List<JuanHaoTable> juanHaoTables)
        {
            if (SysInfo.GetInfo.own != string.Empty)
            {
                if (SysInfo.GetInfo.own != "审核制")
                {
                    反审核(danju.dh);
                }
            }
            Connect.dbcontext.Updateable(danju).ExecuteCommand();
            Connect.dbcontext.Deleteable<danjumingxitable>().Where (x => x.danhao == danju.dh).ExecuteCommand();
            danjumingxitables.ForEach(x => x.danhao = danju.dh);
            Connect.dbcontext.Insertable(danjumingxitables.Where(x => !string.IsNullOrEmpty(x.pingming)).ToList());
            if (SysInfo.GetInfo.own != string.Empty)
            {
                if (SysInfo.GetInfo.own != "审核制")
                {
                    审核(danju.dh);
                }
            }
            //处理卷号
            foreach (var j in juanHaoTables)
            {
                var madan = new FahuoDan()
                {
                    bh = j.SampleNum,
                    pm = j.SampleName,
                    GG = j.guige,
                    gh = j.GangHao,
                    ys = j.yanse,
                    jh = j.JuanHao,
                    mis = j.biaoqianmishu.ToString(),
                    color = j.yanse,
                    Gangjuanhao = j.PiHao.ToString(),
                    dh = danju.dh,
                    rq = danju.rq,
                };
                Connect.dbcontext.Insertable(madan).ExecuteCommand();
                //j.Danhao = danju.dh;
                //JuanHaoTableService.UpdateJuanHaoTable(j, x => x.JuanHao == j.JuanHao);
                j.Danhao = danju.dh;
                j.state = 1;
               
            }
            Connect.dbcontext.Updateable(juanHaoTables).ExecuteCommand();
        }
        public static void 删除单据(string danhao)
        {
            if (SysInfo.GetInfo.own != string.Empty)
            {
                if (SysInfo.GetInfo.own != "审核制")
                {
                    反审核(danhao);
                }
            }
            Connect.dbcontext.Deleteable<DanjuTable>().Where(x => x.dh == danhao).ExecuteCommand ();
            Connect.dbcontext.Deleteable<danjumingxitable>().Where(x => x.danhao == danhao).ExecuteCommand ();
        }
        public static void 审核(string danhao)
        {
            var danju = Connect.dbcontext.Queryable<DanjuTable>().First(x => x.dh == danhao);
            var danjumingxis = Connect.dbcontext.Queryable<danjumingxitable>().Where(x => x.danhao == danhao).ToList();
            danju.zhuangtai = "已审核";
            Connect.dbcontext.Updateable<DanjuTable>(danju).ExecuteCommand();
            库存BLL.减少库存(danjumingxis, danju);
            danju.ckmc = danju.ksmc;
            库存BLL.增加库存(danjumingxis, danju);
        }
        public static void 反审核(string danhao)
        {

            var danju = Connect.dbcontext.Queryable<DanjuTable>().First(x => x.dh == danhao);
            var danjumingxis = Connect.dbcontext.Queryable<danjumingxitable>().Where(x => x.danhao == danhao).ToList();
            danju.zhuangtai = "未审核";
            Connect.dbcontext.Updateable<DanjuTable>(danju).ExecuteCommand();
            库存BLL.增加库存(danjumingxis, danju);
            danju.ckmc = danju.ksmc;
            库存BLL.减少库存(danjumingxis, danju);
            可发卷BLL.卷入库(danhao);
        }
    }
}
